/*
 * 版权所有 (C) 2020 中国南方航空股份有限公司。
 * 本文件可能包含有南方航空公司的机密或专有信息。
 * 未经许可不得擅自公开、复制这些机密资料及其中任何部分，
 * 只可按照其使用许可协议，在南方航空公司内部使用。
 *
 * File Name: $(#)ReprortController.java
 * Creation Date: 2020年9月22日 上午9:14:19
 * $Id$
 */
package com.andy.note.boot.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;

/**
 * <p>
 * DOCUMENTME <code>com.andy.note.boot.controller.ReprortController</code>类的说明，请删除这行并填写正确的描述
 * </p>
 *
 * @author zhouguoan<908119428@qq.com>
 * @version $Revision: 1.1 $ $Date: 2020年9月22日 上午9:14:19 $
 */
@Controller
public class ReprortController {
    
    @Autowired
    private MailAccount account;
    @Autowired
    private JavaMailSender mailSender;
    
    @Autowired
    private TemplateEngine templateEngine;
    @RequestMapping("/report")
    public String report(Model model) throws Exception {
        // 数据组装
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map0 = new HashMap<>();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        map0.put("A", "A0");
        map0.put("B", "B0");
        map0.put("C", "C0");
        map0.put("D", "D0");
        map1.put("A", "A1");
        map1.put("B", "B1");
        map1.put("C", "C1");
        map1.put("D", "D1");
        map2.put("A", "A2");
        map2.put("B", "B2");
        map2.put("C", "C2");
        map2.put("D", "D2");
        map3.put("A", "A3");
        map3.put("B", "B3");
        map3.put("C", "C3");
        map3.put("D", "D3");
        list.add(map0);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        model.addAttribute("list", list);
        
        // SpringMail组装数据发送
        MimeMessage message=mailSender.createMimeMessage();
        Context content = new Context();
        Map<String, Object> all = new HashMap<>();
        all.put("list", list);
        content.setVariables(all);
        MimeMessageHelper helper=new MimeMessageHelper(message,true,"UTF-8");
        helper.setFrom("andychau2015@163.com");
        helper.setTo("908119428@qq.com");
        helper.setSubject("测试模板邮件SpringMail Help组装");
        String process = templateEngine.process("report", content);
        helper.setText(process, true);
        mailSender.send(message);
        
        // hutool工具发送
        MailUtil.send(account, "908119428@qq.com", "hutool工具类发送", process, true);
        
        return "report";
    }
    
}
